package com.atstudy.headline.web.app.service.impl;

import com.atstudy.headline.entity.Comments;
import com.atstudy.headline.entity.Contents;
import com.atstudy.headline.entity.Likes;
import com.atstudy.headline.web.app.mapper.CommentsMapper;
import com.atstudy.headline.web.app.mapper.ContentsMapper;
import com.atstudy.headline.web.app.service.ContentsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author A
* @description 针对表【contents(内容基础信息表)】的数据库操作Service实现
* @createDate 2025-06-12 13:55:56
*/
@Service
public class ContentsServiceImpl extends ServiceImpl<ContentsMapper, Contents>
    implements ContentsService {
    @Autowired
    private ContentsMapper contentsMapper;

    @Autowired
    private CommentsMapper commentsMapper;


    @Override
    public IPage<Contents> pageInfoByContents(IPage<Contents> page) {
        return contentsMapper.pageInfoByContent(page);
    }

    @Override
    public Contents getContentById(Integer contentId) {
        if (contentId != null) {
            // 根据内容id获取评论量
            LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Comments::getContentId, contentId);
            queryWrapper.eq(Comments::getStatus, 1);
            Long count = commentsMapper.selectCount(queryWrapper);

            // 根据内容id获取点赞量
            LambdaQueryWrapper<Comments> likesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            likesLambdaQueryWrapper.eq(Comments::getContentId, contentId);
            likesLambdaQueryWrapper.eq(Comments::getStatus, 1);
            Long likesCount = commentsMapper.selectCount(likesLambdaQueryWrapper);


            // 获取内容对象
            Contents content = contentsMapper.getContentById(contentId);
            if (content != null) {
                // 设置评论量到内容对象
                content.setCommentCount(count.intValue());
                content.setLikeCount(likesCount.intValue());
                // 更新数据库中的评论量
                Contents updateContent = new Contents();
                updateContent.setContentId(contentId);
                updateContent.setCommentCount(count.intValue());
                updateContent.setLikeCount(likesCount.intValue());
                contentsMapper.updateById(updateContent);
            }

            return content;
        }
        return null;
    }

    @Override
    public IPage<Contents> getContentByCategoryId(IPage<Contents> page, Integer categoryId) {
        return contentsMapper.getContentByCategoryId(page,categoryId);
    }

    @Override
    public IPage<Contents> getContentsByUserIdPage(Integer userId, IPage<Contents> page) {

        return this.lambdaQuery()
                .eq(Contents::getUserId, userId)
                .orderByDesc(Contents::getPublishTime) // 按发布时间降序
                .page(page);
    }

    @Override
    public IPage<Contents> getContentByFavorites(IPage<Contents> page, Integer userId) {
        return baseMapper.getContentByFavorites(page,userId);
    }

    @Override
    public IPage<Contents> getHistory(IPage<Contents> page, Integer userId) {
        return baseMapper.getHistory(page,userId);
    }

}




